<?php

namespace origin;

/**
 * 响应类
 * @author dece <decezz@qq.com>
 */
class Response
{
    /**
     * code
     */
    public $code = 200;

    /**
     * header
     */
    public $header = [];

    /**
     * body
     */
    public $content = '';

    /**
     * content type
     */
    public $type = '';

    /**
     * contentType
     */
    protected $contentType = [
        'json'  => 'application/json',
        'html'  => 'text/html',
    ];

    /**
     * 允许跨越请求
     * @access public
     * @param  string $domain 运行跨越的域名(*代表所有)
     * @return $this
     */
    public function cross(string $domain = '*')
    {
        // 原始头
        $cross_header = [
            'Access-Control-Allow-Origin'       => $domain,
            'Access-Control-Allow-Headers'      => 'Origin, X-Requested-With, Content-Type, Accept',
            'Access-Control-Allow-Methods'      => 'POST,GET,DELETE,PUT,PATCH,OPTION',
        ];
        // 设置header
        $this->header($cross_header);

        return $this;
    }

    /**
     * 发送数据
     * @access public
     */
    public function send()
    {
        // 发送状态码
        http_response_code($this->code);
        // response
        if (!headers_sent() && !empty($this->header)) {
            // 设定
            foreach ($this->header as $name => $val) {
                if (is_numeric($name)) {
                    header($val);
                } else {
                    header($name . ':' . $val);
                }
            }
        }

        $this->sendContent();

        if (function_exists('fastcgi_finish_request')) {
            // 提高页面响应
            fastcgi_finish_request();
        }
        exit;
    }

    /**
     * 重定向
     * @access public
     * @param  string $url 地址
     */
    public function redirect(string $url = '')
    {
        header('Location:' . $url);

        exit;
    }

    /**
     * 设置Content-Type
     * @access public
     * @param  string $contentType 输出类型
     * @param  string $charset     输出编码
     * @return $this
     */
    public function contentType(string $type = 'html', string $charset = 'utf-8')
    {
        $this->type = $type;
        if (array_key_exists($type, $this->contentType)) {
            $contentType = $this->contentType[$type];
        } else {
            $contentType = $type;
        }
        // 设置Content-Type
        $this->header['Content-Type'] = $contentType . '; charset=' . $charset;

        return $this;
    }

    /**
     * 发送HTTP状态
     * @access public
     * @param  integer $code 状态码
     * @return $this
     */
    public function code(int $code)
    {
        // 限定数字状态码
        if (100 <= $code && $code <= 600) {
            $this->code = $code;
        }
        return $this;
    }

    /**
     * 设置header
     * @access public
     * @param  array $header header数组
     * @return $this
     */
    public function header(array $header = [])
    {
        // 设置header头
        $this->header = array_merge($this->header, $header);

        return $this;
    }

    /**
     * 获取输出数据
     * @access public
     * @param  mixed $content 数据
     */
    public function content($content)
    {
        if (isset($content)) {
            $this->content = $content;
        }
        return $this;
    }

    /**
     * 输出数据
     * @access protected
     */
    protected function sendContent()
    {
        if ($this->type == 'json' && is_array($this->content)) {
            $this->content = json_encode($this->content);
        }
        echo (string) $this->content;
    }
}
